home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / rw / readWriteXBM.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  9KB  |  346 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <stdio.h>
  16. #include "image.h"
  17.  
  18. int strncmp(char *, char *, int);
  19.  
  20. #define TRUE 1
  21. #define FALSE 0
  22.  
  23. /*********************************************************************
  24. *   Code from MIT
  25. *********************************************************************/
  26.  
  27. /*
  28. Permission to use, copy, modify, distribute, and sell this software and its
  29. documentation for any purpose is hereby granted without fee, provided that
  30. the above copyright notice appear in all copies and that both that
  31. copyright notice and this permission notice appear in supporting
  32. documentation, and that the name of M.I.T. not be used in advertising or
  33. publicity pertaining to distribution of the software without specific,
  34. written prior permission.  M.I.T. makes no representations about the
  35. suitability of this software for any purpose.  It is provided "as is"
  36. without express or implied warranty.
  37. */
  38.  
  39. /*
  40.  *    Code to read bitmaps from disk files. Interprets 
  41.  *    data from X10 and X11 bitmap files and creates
  42.  *    Pixmap representations of files. Returns Pixmap
  43.  *    ID and specifics about image.
  44.  *
  45.  *    Modified for speedup by Jim Becker, changed image
  46.  *    data parsing logic (removed some fscanf()s). 
  47.  *    Aug 5, 1988
  48.  *
  49.  * Note that this file and ../Xmu/RdBitF.c look very similar....  Keep them
  50.  * that way (but don't use common source code so that people can have one 
  51.  * without the other).
  52.  */
  53.  
  54. #include <stdio.h>
  55. #include <ctype.h>
  56.  
  57. extern char *strrchr(char *, char);
  58.  
  59.  
  60. #define MAX_SIZE 255
  61. #define True 1
  62. #define False 0
  63.  
  64. /* shared data for the image read/parse logic */
  65. static short    hexTable[256];        /* conversion value */
  66. static int    initialized = False;    /* easier to fill in at run time */
  67.  
  68.  
  69. /*
  70.  *    Table index for the hex values. Initialized once, first time.
  71.  *    Used for translation value or delimiter significance lookup.
  72.  */
  73. static void initHexTable()
  74. {
  75.     /*
  76.      * We build the table at run time for several reasons:
  77.      *
  78.      *     1.  portable to non-ASCII machines.
  79.      *     2.  still reentrant since we set the init flag after setting table.
  80.      *     3.  easier to extend.
  81.      *     4.  less prone to bugs.
  82.      */
  83.     hexTable['0'] = 0;    hexTable['1'] = 1;
  84.     hexTable['2'] = 2;    hexTable['3'] = 3;
  85.     hexTable['4'] = 4;    hexTable['5'] = 5;
  86.     hexTable['6'] = 6;    hexTable['7'] = 7;
  87.     hexTable['8'] = 8;    hexTable['9'] = 9;
  88.     hexTable['A'] = 10;    hexTable['B'] = 11;
  89.     hexTable['C'] = 12;    hexTable['D'] = 13;
  90.     hexTable['E'] = 14;    hexTable['F'] = 15;
  91.     hexTable['a'] = 10;    hexTable['b'] = 11;
  92.     hexTable['c'] = 12;    hexTable['d'] = 13;
  93.     hexTable['e'] = 14;    hexTable['f'] = 15;
  94.  
  95.     /* delimiters of significance are flagged w/ negative value */
  96.     hexTable[' '] = -1;    hexTable[','] = -1;
  97.     hexTable['}'] = -1;    hexTable['\n'] = -1;
  98.     hexTable['\t'] = -1;
  99.     
  100.     initialized = True;
  101. }
  102.  
  103. /*
  104.  *    read next hex value in the input stream, return -1 if EOF
  105.  */
  106. static int NextInt (fstream)
  107.     FILE *fstream;
  108. {
  109.     int    ch;
  110.     int    value = 0;
  111.     int gotone = 0;
  112.     int done = 0;
  113.     
  114.     /* loop, accumulate hex value until find delimiter  */
  115.     /* skip any initial delimiters found in read stream */
  116.  
  117.     while (!done) {
  118.     ch = getc(fstream);
  119.     if (ch == EOF) {
  120.         value    = -1;
  121.         done++;
  122.     } else {
  123.         /* trim high bits, check type and accumulate */
  124.         ch &= 0xff;
  125.         if (isascii(ch) && isxdigit(ch)) {
  126.         value = (value << 4) + hexTable[ch];
  127.         gotone++;
  128.         } else if ((hexTable[ch]) < 0 && gotone)
  129.           done++;
  130.     }
  131.     }
  132.     return value;
  133. }
  134.  
  135.  
  136. Image *ReadXBM(char *file) 
  137. {
  138.     Image    *image = NULL;
  139.     FILE *fstream;            /* handle on file  */
  140.     unsigned char *data = NULL;        /* working variable */
  141.     char line[MAX_SIZE];        /* input line from file */
  142.     int size;                /* number of bytes of data */
  143.     char name_and_type[MAX_SIZE];    /* an input line */
  144.     char *type;                /* for parsing */
  145.     int value;                /* from an input line */
  146.     int version10p;            /* boolean, old format */
  147.     int padding;            /* to handle alignment */
  148.     int bytes_per_line;            /* per scanline of data */
  149.     unsigned int ww = 0;        /* width */
  150.     unsigned int hh = 0;        /* height */
  151.     int hx = -1;            /* x hotspot */
  152.     int hy = -1;            /* y hotspot */
  153.  
  154.     /* first time initialization */
  155.     if (initialized == False) initHexTable();
  156.  
  157.     if ((fstream = fopen(file, "r")) == NULL)
  158.     return NULL;
  159.  
  160.     /* error cleanup and return macro    */
  161.  
  162.     while (fgets(line, MAX_SIZE, fstream)) {
  163.         if (strlen(line) == MAX_SIZE-1) {
  164.             return NULL;
  165.         }
  166.         if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
  167.             if (!(type = strrchr(name_and_type, '_')))
  168.               type = name_and_type;
  169.             else
  170.               type++;
  171.  
  172.             if (!strcmp("width", type))
  173.               ww = (unsigned int) value;
  174.             if (!strcmp("height", type))
  175.               hh = (unsigned int) value;
  176.             if (!strcmp("hot", type)) {
  177.             if (type-- == name_and_type || type-- == name_and_type)
  178.               continue;
  179.             if (!strcmp("x_hot", type))
  180.               hx = value;
  181.             if (!strcmp("y_hot", type))
  182.               hy = value;
  183.             }
  184.             continue;
  185.         }
  186.  
  187.         if (sscanf(line, "static short %s = {", name_and_type) == 1)    /* } */
  188.           version10p = 1;
  189.         else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1) /* } */
  190.           version10p = 0;
  191.         else if (sscanf(line, "static char %s = {", name_and_type) == 1)    /* } */
  192.           version10p = 0;
  193.         else
  194.           continue;
  195.  
  196.         if (!(type = strrchr(name_and_type, '_')))
  197.             type = name_and_type;
  198.         else
  199.             type++;
  200.  
  201.         if (strcmp("bits[]", type))
  202.             continue;
  203.  
  204.         if (!ww || !hh)
  205.             return NULL;
  206.  
  207.         if ((ww % 16) && ((ww % 16) < 9) && version10p)
  208.             padding = 1;
  209.         else
  210.             padding = 0;
  211.  
  212.         bytes_per_line = (ww+7)/8 + padding;
  213.  
  214.         if ((image = ImageNewBW(ww, hh)) == NULL)
  215.             return NULL;
  216.  
  217.         size = bytes_per_line * hh;
  218.  
  219.         if (version10p) {
  220.             unsigned char *ptr;
  221.             int bytes;
  222.             int    i, x = 0, y = 0;
  223.  
  224.             for (bytes=0, ptr=data; bytes<size; (bytes += 2)) {
  225.                 if ((value = NextInt(fstream)) < 0) {
  226.                     ImageDelete(image);
  227.                     return NULL;
  228.                 }
  229.                 if (!padding || ((bytes+2) % bytes_per_line))
  230.                     value >>= 8;
  231.                 for (i = 0; i < 16 && x < ww; i++, x++)
  232.                     image->data[y * ww + x] = ((value & (1 << i)) != 0) ? 0 : 1;
  233.                 if (x == ww) {
  234.                     y++;
  235.                     x=0;
  236.                 }
  237.             }
  238.         } else {
  239.             unsigned char *ptr;
  240.             int bytes;
  241.             int    i, x = 0, y = 0;
  242.  
  243.             for (bytes=0; bytes<size; bytes++, ptr++) {
  244.                 if ((value = NextInt(fstream)) < 0) {
  245.                     ImageDelete(image);
  246.                     return NULL;
  247.                 }
  248.                 for (i = 0; i < 8 && x < ww; i++, x++)
  249.                     image->data[y * ww + x] = ((value & (1 << i)) != 0) ? 0 : 1;
  250.                 if (x == ww) {
  251.                     y++;
  252.                     x=0;
  253.                 }
  254.             }
  255.         }
  256.     }                    /* end while */
  257.  
  258.     fclose(fstream);
  259.     return image;
  260. }
  261.  
  262. /*********************************************************************
  263. *   End Code from MIT
  264. *********************************************************************/
  265.  
  266. int WriteXBM(char *file, Image *image)
  267. {
  268.     int        x, y, count, idx;
  269.     char        name[256], *cp;
  270.     FILE        *fd;
  271.     unsigned char    val, *dp;
  272.  
  273.     if ((fd = fopen(file, "w")) == NULL)
  274.         return TRUE;
  275.  
  276.     if ((cp = strrchr(file, '/')) == NULL)
  277.         strcpy(name, file);
  278.     else
  279.         strcpy(name, cp+1);
  280.     if ((cp = strrchr(name, '.')) != NULL)
  281.         *cp = '\0';
  282.  
  283.     fprintf(fd, "#define %s_width %d\n", name, image->width);
  284.     fprintf(fd, "#define %s_height %d\n", name, image->height);
  285.     fprintf(fd, "static unsigned char %s_bits[] = {\n  ", name);
  286.  
  287.     dp = image->data;
  288.     for (val = count = y = 0; y < image->height; y++) {
  289.         for (idx = x = 0; x < image->width; x++) {
  290.             val >>= 1;
  291.             if (image->isBW) {
  292.                 val |= *dp++ ? 0 : 0x80;
  293.             } else if (image->isGrey) {
  294.                 val |= (*dp++ > 128) ? 0 : 0x80;
  295.             } else {
  296.                 dp = ImagePixel(image, x, y);
  297.                 val |= (((float)dp[0] * 0.4 +
  298.                           (float)dp[1] * 0.4 +
  299.                           (float)dp[2] * 0.2) > 128) ? 0 : 0x80;
  300.             }
  301.             idx++;
  302.             if (idx == 8) {
  303.                 idx = 0;
  304.                 fprintf(fd, "0x%02x", val);
  305.  
  306.                 if ((++count % 12) == 0)
  307.                     fprintf(fd, ",\n  ");
  308.                 else
  309.                     fprintf(fd, ", ");
  310.             }
  311.         }
  312.         if (idx != 0) {
  313.             val >>= 8 - idx;
  314.             fprintf(fd, "0x%02x", val);
  315.  
  316.             if ((++count % 12) == 0)
  317.                 fprintf(fd, ",\n  ");
  318.             else
  319.                 fprintf(fd, ", ");
  320.         }
  321.     }
  322.  
  323.     fprintf(fd, "};\n");
  324.  
  325.     fclose(fd);
  326.  
  327.     return FALSE;
  328. }
  329.  
  330. int TestXBM(char *file)
  331. {
  332.     FILE    *fd = fopen(file, "r");
  333.     char    buf[20];
  334.     int    ret = FALSE;
  335.  
  336.     if (fd == NULL)
  337.         return FALSE;
  338.  
  339.     fread(buf, sizeof(char), 20, fd);
  340.     fclose(fd);
  341.  
  342.     ret = (strncmp("#define", buf, 7) == 0);
  343.  
  344.     return ret;
  345. }
  346.